package leetcode_day._2021._202104._2131;

import general_class.TreeNode;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @author yzh
 * @version 1.0
 * @date 2021/4/27 8:42
 * 层次遍历，也叫广度优先搜索
 * 使用队列
 */
public class _27_938 {

    public int rangeSumBST(TreeNode root, int low, int high) {
        if (null == root) return 0;
        int ans = 0;
        Deque<TreeNode> queue = new ArrayDeque<>();
        queue.addLast(root);
        while (!queue.isEmpty()) {
            TreeNode treeNode = queue.pollFirst();
            if (treeNode.val >= low && treeNode.val <= high) ans += treeNode.val;
            if (null != treeNode.left) queue.addLast(treeNode.left);
            if (null != treeNode.right) queue.addLast(treeNode.right);
        }
        return ans;
    }
}
